/*************************************************************************
 *                  HONGLING CAPITAL CONFIDENTIAL AND PROPRIETARY
 *
 *                COPYRIGHT (C) HONGLING CAPITAL CORPORATION 2012
 *    ALL RIGHTS RESERVED BY HONGLING CAPITAL CORPORATION. THIS PROGRAM
 * MUST BE USED  SOLELY FOR THE PURPOSE FOR WHICH IT WAS FURNISHED BY
 * HONGLING CAPITAL CORPORATION. NO PART OF THIS PROGRAM MAY BE REPRODUCED
 * OR DISCLOSED TO OTHERS,IN ANY FORM, WITHOUT THE PRIOR WRITTEN
 * PERMISSION OF HONGLING CAPITAL CORPORATION. USE OF COPYRIGHT NOTICE
 DOES NOT EVIDENCE PUBLICATION OF THE PROGRAM.
 *                  HONGLING CAPITAL CONFIDENTIAL AND PROPRIETARY
 *************************************************************************/

package com.springboot.common.jms;

import com.alibaba.fastjson.JSONObject;
import org.apache.activemq.command.ActiveMQDestination;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jms.*;
import java.util.HashMap;
import java.util.Map;

/**
 * activemq的消息队列监听类。
 *
 * @author hongchao zhao at 2019-5-23 16:25
 */
public class QueueMessageListener implements MessageListener {

    private static Logger LOG = LoggerFactory.getLogger(QueueMessageListener.class);

    @Override
    public void onMessage(Message message) {
        try {
            ActiveMQDestination queues = (ActiveMQDestination) message.getJMSDestination();
            LOG.debug("线程【{}】监听队列【{}】消费了消息:", Thread.currentThread().getName(), queues.getPhysicalName());
            // 如果是文本消息
            doTxtMessage(message);

            // 如果是Map消息
            doMapMessage(message);

        } catch (JMSException e1) {
            e1.printStackTrace();
        }
    }

    /**
     * 处理Map类消息请求。
     *
     * @param message 消息。
     */
    private void doMapMessage(Message message) {
        if (message instanceof MapMessage) {
            try {
                MapMessage mm = (MapMessage) message;
                String messageKey = mm.getString("key");
                String messageData = mm.getString("data");
                LOG.debug("获取当前消息key===>【{}】，data ==>【{}】。", messageKey, messageData);
                //处理业务逻辑。。。
                if (StringUtils.equals(messageKey, QueueMessage.UPDATE_TOPIC_READ_REPLY_COUNT)) {
                    //将JSON转为Map对象
                    JSONObject jsonObject = JSONObject.parseObject(messageData);
                    Map params = JSONObject.toJavaObject(jsonObject, Map.class);
                    //更新浏览量和帖子回复数
                    LOG.debug("获取信息【{}】，处理业务中。。。。", params);

                } else {
                    LOG.debug("未找到当前业务方法：key===>【{}】，data ==>【{}】。", messageKey, messageData);
                }
                //确认消息已处理，防止消息丢失
                mm.acknowledge();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 处理文本类消息请求。
     *
     * @param message 消息。
     */
    private void doTxtMessage(Message message) {
        if (message instanceof TextMessage) {
            TextMessage tm = (TextMessage) message;
            try {
                System.out.println("from get textMessage：\t" + tm.getText());
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }
}
